package com.greyshark.sanotrive.msgrelay

import android.os.Bundle
import android.view.View
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyEvents
import com.common.base.app.activity.BaseVBActivity
import com.common.base.app.extras.no
import com.common.base.app.extras.startActivity
import com.common.base.utils.ActivityCollector.checkHasActivity
import com.jeremyliao.liveeventbus.LiveEventBus
import com.greyshark.sanotrive.databinding.ActivityCommonNotificationTransitBinding
import com.greyshark.sanotrive.splash.SplashActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.CommonMessageRelayModel
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2023/2/5
 *
 * 描述：外部访问中转页
 *
 * 修订历史：
 *
 */
class CommonMessageRelayActivity : BaseVBActivity<ActivityCommonNotificationTransitBinding>(),
    View.OnClickListener {

    override fun getViewBinding(): ActivityCommonNotificationTransitBinding =
        ActivityCommonNotificationTransitBinding.inflate(layoutInflater)

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews()

    }

    override fun lazyInitData() {
        super.lazyInitData()
        var curPath = ""
        var webType = 0
        val uri = intent.data
        intent.data?.host
        val parametersMap = HashMap<String, String>()
        if (uri != null) {//深链参数处理
            val queryParameterNames = uri.queryParameterNames
            curPath = uri.getQueryParameter("curPath") ?: ""
            webType = (uri.getQueryParameter("webType") ?: "0").toInt()
            for (key in queryParameterNames) {
                val value = uri.getQueryParameter(key)
                parametersMap[key] = if (value.isNullOrEmpty()) "" else value
            }
        }

//        Log.e("CommonMessageRelayActivity-Tag", "msg: ${uri.toString()}")

        val hasMain = checkHasActivity("MainActivity")

        hasMain.no {
            val bundle = Bundle()
            bundle.putParcelable(
                KeyBundle.BUNDLE_KEY,
                CommonMessageRelayModel(curPath, webType, parametersMap)
            )
            startActivity<SplashActivity>(bundle, true)
            return
        }

        (curPath.isNullOrEmpty()).no {
            if ("null" == curPath) {
                finish()
                return
            }

            curPath.contains("Fragment").yes {
                when(curPath){
                    RouterHub.ROUTER_HOME_FRAGMENT-> LiveEventBus.get(RouterHub.ROUTER_HOME_FRAGMENT).post(this)
                    RouterHub.ROUTER_CIRCLE_FRAGMENT-> LiveEventBus.get(RouterHub.ROUTER_CIRCLE_FRAGMENT).post(this)
                    RouterHub.ROUTER_ME_FRAGMENT-> LiveEventBus.get(RouterHub.ROUTER_ME_FRAGMENT).post(this)
                }
                finish()
                return
            }

            showToast("正在跳转页面,请稍等~")

            LiveEventBus.get(
                KeyEvents.EVENT_TYPE_MESSAGE_RELAY_DATA,
                CommonMessageRelayModel::class.java
            ).post(CommonMessageRelayModel(curPath, webType, parametersMap))
        }

        finish()
    }

    override fun initEvents() {
        super.initEvents()
    }

    override fun onClick(v: View?) {

    }
}